home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / .bin / httpd / Solaris_x86 / mail < prev    next >
Text File  |  1995-05-18  |  12KB  |  320 lines

  1. #!/usr/local/bin/perl 
  2.  
  3. # ======================================================================
  4. # WebMonitor Standalone Module: mail
  5. #
  6. #                               CGI script for providing form and script
  7. #                                to send mail to configured system users
  8. #
  9. # required files: mail.list
  10. #                                     Text file with users nicknames and
  11. #                                       email addresses in the format of
  12. #                                             <nickname>:<email address>
  13. #                      Keep "mail.list" in same directory as mail script
  14. #                                 NOTE: you can even have group aliases!
  15. #                                just separate the addresses with commas
  16. #         Make sure you 'chmod 0644 mail.list' so the server can read it
  17. #                             +-----------------------------------------
  18. #                   Example:  |webmaster:admin@machine
  19. #                             |john-doe:jdoe
  20. #                             |carlos:cpero@ncsa.uiuc.edu
  21. #                             |group:leader@domain.com,member@domain.com
  22. #                             +-----------------------------------------
  23. # ======================================================================
  24. # Carlos A. Pero (cpero@ncsa.uiuc.edu)              last update  4/30/95
  25. # ======================================================================
  26. # Documentation for WebMonitor can be found at
  27. #                          <URL:http://hoohoo.ncsa.uiuc.edu/webmonitor/>
  28. # ======================================================================
  29. # This code is in the public domain. Specifically, we give to the public
  30. # domain all rights for future licensing of the source code, all resale
  31. # rights, and all publishing rights.
  32. # We ask, but do not require, that the following message be included in
  33. # all derived works:
  34. # Portions developed at the National Center for Supercomputing
  35. # Applications at the University of Illinois at Urbana-Champaign.
  36. #
  37. # THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED,
  38. # FOR THE SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT
  39. # LIMITATION, WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A
  40. # PARTICULAR PURPOSE.
  41. # ======================================================================
  42. # This script can be referenced 2 ways for the best flexibility:
  43. #
  44. # DIRECTLY, <A HREF="/cgi-bin/mail?nickname">
  45. # This will generate an email form for the person named in 'nickname',
  46. # and if they exist in the 'mail.list' file.
  47. # If no 'nickname' is specified in the QUERY_STRING when the script is
  48. # first invoked, or the nickname cannot be found in the 'mail.list', 
  49. # an email form with a SELECT box of all valid nicknames is generated.
  50. # When the email form is submitted, it will call itself via method of POST,
  51. # and send the email to the recipient, outputting a confirmation message.
  52. # If the HTTP_REFERER was trasmitted when the script was first invoked,
  53. # there will be a hyperlink available to go back to that page (such as
  54. # the user's home page).
  55. #
  56. # FORWARDING RESULTS, <FORM METHOD="POST" ACTION="/cgi-bin/mail?nickname">
  57. # This will forward the results from the FORM, which can exist anywhere,
  58. # to the recipient specified by 'nickname'.  Since the 'nickname' is in
  59. # the QUERY_STRING, the FORM *must* use the METHOD="POST", otherwise the
  60. # recipient's nickname will be blown away.
  61. # Users may want to include a:
  62. #    <INPUT TYPE="hidden" NAME="next-url" VALUE="/~user/received.html">
  63. # If this is present in the FORM input, the client will be redirected
  64. # to this HTML file as a confirmation message instead of the default.
  65. # In addition, the user can also define any of the following input names
  66. # in their form to better customize the output mailed back to them.
  67. #    <INPUT TYPE="hidden" NAME="subject" VALUE="My survey results">
  68. #    <INPUT TYPE="hidden" NAME="from-name" VALUE="Average Web user">
  69. #    <INPUT TYPE="hidden" NAME="from-email" VALUE="jdoe@domain.com">
  70. # These values will then be used in the header of the email message.
  71. # Otherwise, default values will be substituted.
  72. # ======================================================================
  73.  
  74.  
  75. ########################################################################
  76. ########## Configurable variables ######################################
  77.  
  78. $SENDMAIL = '/usr/sbin/sendmail';
  79. #                                   The location of your sendmail binary
  80.  
  81. ## Also, make sure the first line of this script points
  82. ## to your PERL binary
  83.  
  84. ########## Nothing else to change ######################################
  85. ########################################################################
  86.  
  87.  
  88. $SCRIPT = $ENV{'SCRIPT_NAME'};
  89.  
  90. #### Do standard HTTP stuff ####
  91. &cgi_receive;
  92. &cgi_decode;
  93. &cgi_header;
  94.  
  95. #### Output email form unless you are receiving FORM input already ####
  96. &print_form unless keys %FORM;
  97.  
  98. #### Make sure required fields are filled out ####
  99. if ($FORM{'nickname'}) {
  100.     #### This section is for receiving from this form ####
  101.     &error_blank_field('your name')  unless ($FORM{'from-name'});
  102.     &error_blank_field('your email address in the form of <I>name@domain</I>') unless ($FORM{'from-email'} =~ /\@/);
  103.     &error_blank_field('the subject of the message') unless ($FORM{'subject'});
  104.     &error_blank_field('your message') unless ($FORM{'message'});
  105. }
  106. else {
  107.     #### This section is for user's forms ####
  108.     #### Such as ACTION="/cgi-bin/mail?carlos" to have results forwarded ####
  109.     $FORM{'nickname'} = $ENV{'QUERY_STRING'};
  110. }
  111.  
  112. #### Lookup email address based on 'nickname' ####
  113. open (MAILNAMES, "mail.list") || die ("$SCRIPT: Can't open mail.list: $!\n");
  114. while (<MAILNAMES>) {
  115.     chop;
  116.     ($nick, $addr) = split(/:/, $_);
  117.     $ADDRESS{$nick} = $addr;
  118. }
  119. close (MAILNAMES);
  120. &error_blank_field('the nickname of the recipient') unless ($ADDRESS{$FORM{'nickname'}});
  121.  
  122. #### Make sure all necessary variables for email message are filled in
  123. ($FORM{'subject'}) || ($FORM{'subject'} = "FORM results");
  124. ($FORM{'from-email'}) || ($FORM{'from-email'} = $ADDRESS{$FORM{'nickname'}});
  125. ($FORM{'from-name'}) || ($FORM{'from-name'} = "WebMonitor mail");
  126.  
  127. open (MAIL, "| $SENDMAIL $ADDRESS{$FORM{'nickname'}}") || die ("$SCRIPT: Can't open $mailprog: $!\n");
  128. print MAIL "Reply-to: $FORM{'from-email'} ($FORM{'from-name'})\n";
  129. print MAIL "From: $FORM{'from-email'} ($FORM{'from-name'})\n";
  130. print MAIL "To: $ADDRESS{$FORM{'nickname'}}\n";
  131. print MAIL "Subject: $FORM{'subject'}\n";
  132. print MAIL "\n";
  133. print MAIL "=============================================================\n";
  134. print MAIL "NOTE:  This message was sent through the WebMonitor mail form\n";
  135. print MAIL "=============================================================\n";
  136. print MAIL "REMOTE HOST:        $ENV{'REMOTE_HOST'}\n";
  137. print MAIL "REMOTE ADDRESS:     $ENV{'REMOTE_ADDR'}\n";
  138. print MAIL "HTTP_USER_AGENT:    $ENV{'HTTP_USER_AGENT'}\n";
  139. print MAIL "=============================================================\n";
  140. print MAIL "\n";
  141. if ($ENV{'QUERY_STRING'}) {
  142.     &dump_values(FORM, MAIL);
  143. }
  144. else {
  145.     print MAIL "$FORM{'message'}\n";
  146. }
  147. print MAIL "\n";
  148. close (MAIL);
  149.  
  150. #### Now, redirect if "next-url" is included
  151. if ($FORM{'next-url'}) {
  152.     print "Location: $FORM{'next-url'}\n";
  153.     print "\n";
  154.     exit;
  155. }
  156.  
  157. #### Prevent HTML output
  158. foreach $key (keys %FORM) {
  159.     $FORM{$key} =~ s/</\</g;
  160.     $FORM{$key} =~ s/>/\>/g;
  161. }
  162.  
  163. #### Output confirmation message ####
  164. print qq|<HTML><HEAD><TITLE>WebMonitor-Email Sent</TITLE></HEAD><BODY>\n|;
  165. print qq|<H1>$ENV{'SERVER_NAME'} Email Sent</H1>\n|;
  166. print qq|The following message has been sent.\n|;
  167. print qq|You can now return to <A HREF="$FORM{'previous-url'}">where you were</A>.\n| if ($FORM{'previous-url'});
  168. print qq|<HR>\n|;
  169. print "<PRE>\n";
  170. print "Reply-to: $FORM{'from-email'} ($FORM{'from-name'})\n";
  171. print "From: $FORM{'from-email'} ($FORM{'from-name'})\n";
  172. print "To: $ADDRESS{$FORM{'nickname'}}\n";
  173. print "Subject: $FORM{'subject'}\n";
  174. print "\n";
  175. if ($ENV{'QUERY_STRING'}) {
  176.     &dump_values(FORM, STDOUT);
  177. }
  178. else {
  179.     print "$FORM{'message'}\n";
  180. }
  181. print "\n";
  182. print "</PRE>\n";
  183. print "</BODY>\n";
  184. exit;
  185.  
  186. #####################################################################
  187. #### SUBROUTINES ####################################################
  188.  
  189. sub error_blank_field {
  190.     local($variable) = @_;
  191.     print "\n" if ($FORM{'next-url'});
  192.     print "<HTML><HEAD><TITLE>WebMonitor-Email Error</TITLE></HEAD><BODY>\n";
  193.     print "<H1>Error!</H1>\n";
  194.     print "You did not fill in $variable.\n";
  195.     print "Please go back to the form and do so.\n";
  196.     print "</BODY>\n";
  197.     exit;
  198. }
  199.  
  200. sub cgi_header {
  201.     print "Content-type: text/html\n";
  202.     print "\n" unless ($FORM{'next-url'});
  203. }        
  204.  
  205. sub cgi_receive {
  206.     if ($ENV{'REQUEST_METHOD'} eq "POST") {
  207.         read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
  208.     }
  209.     else {
  210.         $incoming = $ENV{'QUERY_STRING'};
  211.     }
  212. }
  213.  
  214. sub cgi_decode {
  215.     @pairs = split(/&/, $incoming);
  216.  
  217.     foreach (@pairs) {
  218.         ($name, $value) = split(/=/, $_);
  219.  
  220.         $name  =~ tr/+/ /;
  221.         $value =~ tr/+/ /;
  222.         $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
  223.         $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
  224.  
  225.         #### Strip out semicolons unless for special character
  226.         $value =~ s/;/$$/g;
  227.         $value =~ s/&(\S{1,6})$$/&\1;/g;
  228.         $value =~ s/$$/ /g;
  229.  
  230.         $value =~ s/\|/ /g;
  231.         $value =~ s/^!/ /g; ## Allow exclamation points in sentences
  232.  
  233.         #### Skip blank text entry fields
  234.         next if ($value eq "");
  235.  
  236.         #### Check for "assign-dynamic" field names
  237.         #### Mainly for on-the-fly input names, especially checkboxes
  238.         if ($name =~ /^assign-dynamic/) {
  239.             $name = $value;
  240.             $value = "on";
  241.         }
  242.  
  243.     #### Allow for multiple values of a single name
  244.         $FORM{$name} .= ", " if ($FORM{$name});
  245.  
  246.         $FORM{$name} .= $value;
  247.     }
  248. }
  249.  
  250. sub dump_values {
  251.     local($env, $handle) = @_;
  252.     eval "\@keys = keys \%$env";
  253.     eval "\@values = values \%$env";
  254.     ($handle eq "STDOUT") && (print $handle "<PRE>\n");
  255.     while ($#keys >= 0) {
  256.     $key = pop(@keys);
  257.     $value = pop(@values);
  258.     if ($value =~ /[\cM\n]/) {
  259.         print $handle "($key)\n";
  260.         print $handle "-" x 70, "\n", $value, "-" x 70, "\n";
  261.     }
  262.     else {
  263.             print $handle "($key)  $value\n";
  264.     }
  265.     }
  266.     ($handle eq "STDOUT") && (print $handle "</PRE>\n");
  267.  
  268. }
  269.  
  270. sub print_form {
  271.     #### Assign path_info and query_string if necessary
  272.     #### $path_info = "";
  273.  
  274.     open (MAILNAMES, "mail.list") || die ("$SCRIPT: Can't open mail.list: $!\n");
  275.     while (<MAILNAMES>) {
  276.     chop;
  277.     ($nickname, $address) = split(/:/, $_);
  278.     $ADDRESS{$nickname} = $address;
  279.     }
  280.     close (MAILNAMES);
  281.  
  282.     print qq|<HTML><HEAD><TITLE>WebMonitor-Email Form</TITLE></HEAD><BODY>\n|;
  283.     print qq|<H1>$ENV{'SERVER_NAME'} <A HREF="http://hoohoo.ncsa.uiuc.edu/webmonitor/module-mail.html">Email Form</A></H1>\n|;
  284.     print qq|<FORM METHOD="POST" ACTION="$SCRIPT$path_info$query_string">\n|;
  285.     print qq|<HR>\n|;
  286.     print qq|<INPUT TYPE="submit" VALUE="Send Email"> to |;
  287.  
  288.  
  289.     if ($ADDRESS{$ENV{'QUERY_STRING'}}) {
  290.     print qq|<B>$ENV{'QUERY_STRING'}</B> <I>($ADDRESS{$ENV{'QUERY_STRING'}})</I>\n|;
  291.     print qq|<INPUT TYPE="hidden" NAME="nickname" VALUE="$ENV{'QUERY_STRING'}">\n|;
  292.     }
  293.     else {
  294.     print qq|<SELECT NAME="nickname">\n|;
  295.     print qq|<OPTION>Select name...\n|;
  296.         foreach $nickname (sort keys %ADDRESS) {
  297.         print qq|<OPTION>$nickname\n|;
  298.         }
  299.         print qq|</SELECT>\n|;
  300.     }
  301.  
  302.  
  303.     print qq|<HR>\n|;
  304.     print qq|<PRE>|;
  305.     print qq|    Your Name: <INPUT NAME="from-name" SIZE="30">\n|;
  306.     print qq|Email Address: <INPUT NAME="from-email" SIZE="30">\n|;
  307.     print qq|      Subject: <INPUT NAME="subject" SIZE="40">    <INPUT TYPE="reset" VALUE="Clear Message">\n|;
  308.     print qq|</PRE>\n|;
  309.     print qq|<TEXTAREA NAME="message" ROWS="15" COLS="70"></TEXTAREA>\n|;
  310.     print qq|<INPUT TYPE="hidden" NAME="previous-url" VALUE="$ENV{'HTTP_REFERER'}">\n|;
  311.     print qq|</FORM>\n|;
  312.     print qq|</BODY>\n|;
  313.  
  314.     exit;
  315. }
  316.  
  317.